<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用ES的Promise机制模拟用户远程登录验证</title>
</head>
<body>
    <script>

        const users = [
            { username: "zs", password: "123456" },
            { username: "ls", password: "123456" },
            { username: "wr", password: "123456" }
        ]
        function auth(username, password) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    const user = users.find(user => user.username === username && user.password === password)
                    if (user) {
                        resolve(user)
                    } else {
                        reject('用户名或密码错误')
                    }
                }, 2000)
            })
        }
        auth('zs', '1234567')
            .then(user => console.log('welcome ' + user.username))
            .catch(error => console.error(error.message))
    </script>
</body>

</html>